package com.zmn.plat.admin.controller.extension.serv.category;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategory;

/**
 * 服务推广分类
 *
 * @author linfeng
 * @since 2019-12-24 10:55:48
 */
@RestController
@RequestMapping("/extension/serv/category/")
public class ExtensionServCategoryModifyController extends ExtensionServCategoryBaseController {

    /**
     * 新增修改
     *
     * @param extensionServCategory
     * @author linfeng
     * @since 2019-12-24 10:55:48
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/extension/serv/category/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ExtensionServCategory extensionServCategory) {
        try {
            extensionServCategoryBService.addEdit(extensionServCategory, getMcStaffRealName());
            return ResponseDTO.success(Integer.toString(extensionServCategory.getCategId()), "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("不能重复添加！", extensionServCategory.getName());
            return ResponseDTO.fail("名称重复，不能重复添加！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改推广服务分类失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author linfeng
     * @since 2019-12-24 10:55:48
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/extension/serv/category/list.action")
    @SuppressWarnings("Duplicates")
    public ResponseDTO updateSort(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        Integer result = extensionServCategoryService.updateSortByKey(id, sort, getMcStaffRealName());

        return ResponseDTO.success(result, "操作成功！");
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @author linfeng
     * @since 2019-12-24 10:55:48
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/extension/serv/category/updateStatus.action")
    @SuppressWarnings("Duplicates")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (status == 0) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        Integer result = extensionServCategoryService.updateStatusBatchByKey(arrayIds, status, getMcStaffRealName());

        return ResponseDTO.success(result, "操作成功！");
    }

    /**
     * 描述:修改首字母
     *
     * @param categId
     *            主键id
     * @param firstLetter
     *            首字母
     * @return: ResultDTO 执行结果
     * @auther: linfeng
     * @since:20191224
     */
    @RequestMapping(value = "updateFirstLetter")
    @RequiresPermissions("/extension/serv/category/list.action")
    @SuppressWarnings("Duplicates")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer categId,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {

        if (categId == 0) {
            return ResponseDTO.fail("请选择要更改首字母的表名称！");
        }
        if (StringUtil.isBlank(firstLetter)) {
            return ResponseDTO.fail("请设置分类首字母！");
        }
        Integer result = extensionServCategoryService.updateFirstLetterByKey(categId, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }
}
